package icbc.nav.control
{
	import fl.containers.ScrollPane;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import flashx.textLayout.formats.TextAlign;
	
	import icbc.app.AppGlobal;
	
	public class TooltipControl extends Sprite
	{
		public static var EVENT_CLICK:String = "TIP_EVENT_CLICK";
		private static const TXT_CHILD_NAME:String = "txt";
		private static const FONT_SIZE:uint = 16;
		private static const FONT_COLOR:uint = 0xE381B2;
		private static const TEXT_HEIGHT:uint = 27;
		
		[Embed (source="/assets/tip_close.png")]
		public var tipCloseImage:Class;

		protected var panel:Sprite;
		public var viewWidth:uint;
		public var viewHeight:uint;

		private var gap:uint = 5;
		public var data:Object;
		private var controlWidth:uint;
		private var controlHeight:uint;
		private var closeImage:Sprite;
		private var rounded:uint = 20;
		
		private var scrollpane:ScrollPane;
		private var place:Sprite;
		private var scrollPaneWidth:uint = 290;
		private var scrollPaneHeight:uint = 440;
		
		public function TooltipControl(data:Object, vWidth:uint = 300, vHeight:uint = 250)
		{
			super();
			this.data = data;
			viewWidth = vWidth;
			viewHeight = vHeight;
			scrollPaneWidth = viewWidth - 10;
			scrollPaneHeight = viewHeight - 10;
			this.controlWidth = vWidth;
			this.controlHeight = vHeight;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(Event.RESIZE, initMain);
			stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function initMain(evt:Event = null):void
		{
			if(stage.stageWidth>0)
				stage.removeEventListener(Event.RESIZE, initMain);
			else return;
			initContent();
		}
		
		private function initContent():void
		{
			panel = setBasePanel();
			addChild(panel);
			
			buildCloseIcon();
			
			initScrollpane();
		}
		
		private function buildCloseIcon():void
		{
			var iconClose:Bitmap = new tipCloseImage();
			closeImage = new Sprite();
			closeImage.addChild(iconClose);
			panel.addChild(closeImage);
			closeImage.addEventListener(MouseEvent.CLICK, closeHandler);
			var gapx:uint = 25;
			var gapy:uint = 5;
			closeImage.x = panel.width - gapx;
			closeImage.y = gapy;
		}
		
		private function setBasePanel():Sprite
		{
			var base:Sprite = new Sprite();
			base.graphics.beginFill(0xE6EFFC);
			base.graphics.drawRoundRect(0, 0, viewWidth, viewHeight, rounded, rounded);
			base.graphics.endFill();
			var filterD:DropShadowFilter = new DropShadowFilter(5, 45, 0xa39a95, .7, 4, 4, 1, 3, false, false, false);
			var filterB:BevelFilter = new BevelFilter(1, 45, 0xffffff, 1, 0x5c5c5c, 1, 2, 2, 2, 3, "inner", false);
			var filterG1:GlowFilter = new GlowFilter(0x188CF8, 1, 2, 2, 1, 3, false, false);
			var filterG2:GlowFilter = new GlowFilter(0xffffff, 1, 5, 5, 1, 3, false, false);
			base.filters = [filterG1, filterB, filterD, filterG2];
			return base;
		}
		
		private function initScrollpane():void
		{
			var gapy:uint = 20;
			var controlWidth:uint = scrollPaneWidth;
			var controlHeight:uint = scrollPaneHeight - gapy;
			
			scrollpane = new ScrollPane();
			scrollpane.setSize(controlWidth, controlHeight);
			scrollpane.horizontalScrollPolicy = "off";
			var newSkinClip:Sprite = new Sprite();
			newSkinClip.graphics.clear();
			newSkinClip.graphics.beginFill(0xff0000, 0);
			newSkinClip.graphics.drawRect(0,0,272.0, 191.0);
			newSkinClip.graphics.endFill();
			scrollpane.setStyle("skin", newSkinClip );
			scrollpane.setStyle("upSkin", newSkinClip );
			scrollpane.setStyle("borderColor", 0xFFFFFF);
			scrollpane.setStyle("borderStyle","none");
			scrollpane.drawFocus(false);
			scrollpane.focusRect = null;
			scrollpane.tabEnabled = false;
			panel.addChild(scrollpane);
			
			scrollpane.x = viewWidth/2 - scrollPaneWidth/2;
			scrollpane.y = viewHeight/2 - scrollPaneHeight/2 + gapy;
			
			place = new Sprite();
			scrollpane.source = place;
			initPlace();
		}
		
		private function initPlace():void
		{
			var textfield:TextField =  buildContentText(data.text);
			place.addChild(textfield);
			scrollpane.refreshPane();
			scrollpane.invalidate();
		}
		
		private function buildContentText(text:String):TextField
		{
			var txtWidth:uint = scrollPaneWidth-15;
			var txtHeight:uint = 400;
			var format:TextFormat = new TextFormat();
			format.font = AppGlobal.FONT_NAME;
			format.color = 0x000000;
			format.size = 16;
			format.align = TextAlign.LEFT;
			var txt:TextField = new TextField();
			txt.selectable = false;
			txt.width = txtWidth;
			txt.height = txtHeight;
			txt.defaultTextFormat = format;
			txt.htmlText = text;
			txt.wordWrap = true;
			txt.border = false;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.borderColor = 0xDDDDDD;
			return txt;
		}
		
		private function closeHandler(evt:MouseEvent = null):void
		{
			closeImage.removeEventListener(MouseEvent.CLICK, closeHandler);
			parent.removeChild(this);
		}

		public function dispose():void
		{
			closeHandler();
		}
	}
}